import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'providers/ThemeDataBloc.dart';
import 'providers/CounterBloc.dart';

class MultiProviderDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
      theme: Provider.of<ThemeData>(context),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('MultiProviderWithStream'),
      ),
      body: Center(
        child: StreamBuilder(
          //StreamBuilder来监听Stream
          builder: (context, snapshot) {
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                    icon: Icon(Icons.add),
                    onPressed: () {
                      Provider.of<CounterBloc>(context).addCounter();
                    }),
                CircleAvatar(
                  child: Text(snapshot.data.toString()),
                ),
                IconButton(
                    icon: Icon(Icons.remove),
                    onPressed: () {
                      Provider.of<CounterBloc>(context).subCounter();
                    }),
              ],
            );
          },
          initialData: 0,
          stream: Provider.of<CounterBloc>(context).stream,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          ThemeDataBloc().changeTheme();
        },
        child: Icon(Icons.change_history),
      ),
    );
  }
}
